package com.atguigu.spzx.manager.controller;

import com.atguigu.spzx.manager.service.SysRoleService;
import com.atguigu.spzx.model.dto.system.AssginRoleDto;
import com.atguigu.spzx.model.dto.system.SysRoleDto;
import com.atguigu.spzx.model.entity.system.SysRole;
import com.atguigu.spzx.model.vo.common.Result;
import com.atguigu.spzx.model.vo.common.ResultCodeEnum;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/admin/system/sysRole")
@Slf4j
public class SysRoleController {

    @Autowired
    private SysRoleService sysRoleService;

    @PostMapping("/findByPage/{current}/{limit}")
    public Result findByPage(@PathVariable Integer current, @PathVariable Integer limit ,SysRoleDto sysRoleDto){
        log.info("查询角色列表，参数：current={},limit={},sysRoleDto={}", current, limit, sysRoleDto);
        PageInfo<SysRole> page = sysRoleService.findByPage(current, limit,sysRoleDto);
        return Result.build(page, ResultCodeEnum.SUCCESS);
    }

    //TODO: 重置

    @PostMapping("/saveSysRole")
    public Result saveSysRole(@RequestBody SysRole sysRole){
        log.info("添加角色，参数：sysRole={}", sysRole);
        sysRoleService.saveSysRole(sysRole);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }


    @PutMapping("/updateSysRole")
    public Result updateSysRole(@RequestBody SysRole sysRole){
        log.info("修改角色，参数：sysRole={}", sysRole);
        sysRoleService.updateSysRole(sysRole);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    @DeleteMapping("/deleteById/{roleId}")
    public Result deleteById(@PathVariable("roleId") Long roleId){
        log.info("删除角色，参数：roleId={}", roleId);
        sysRoleService.deleteById(roleId);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    @GetMapping("/findAllRoles/{userId}")
    public Result findAllRoles(@PathVariable Long userId){
        Map<String, Object> map =sysRoleService.findAllRoles(userId);
        return Result.build(map,ResultCodeEnum.SUCCESS);
    }


}
